package com.java.util;

import org.apache.commons.lang.StringUtils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * HttpRequest.Parameter 정보를 관리하는 Warapper 클래스
 * 
 * @author <a href="mailto:lamp_java@naver.com">Lamp</a>
 */
public class RequestMap extends HashMap implements HttpMap {

	private static final long serialVersionUID = 910418938559308357L;

	/**
	 * @param initialCapacity
	 * @param loadFactor
	 */
	public RequestMap(int initialCapacity, float loadFactor) {
		super(initialCapacity, loadFactor);
	}

	/**
	 * @param initialCapacity
	 */
	public RequestMap(int initialCapacity) {
		super(initialCapacity);
	}

	public RequestMap() {
		super();
	}

	public RequestMap(Map map) {
		super(map);
	}

	/**
	 * @param key
	 * @return
	 * @see com.lamp.common.HttpMap#getString(String)
	 */
	public String getString(String key) {
		try {
			return (String) get(key);
		} catch (Exception e) {
			Object o = get(key);
			if (o instanceof String[]) {
				String[] values = (String[]) o;
				if (values != null) {
					return values[0];
				}
			}
			return null;
		}
	}

	/**
	 * @param key
	 * @return
	 * @see com.lamp.common.HttpMap#getStrings(String)
	 */
	public String[] getStrings(String key) {
		Object value = get(key);

		try {
			if (value instanceof String[]) {
				return (String[]) value;
			} else {
				String[] values = { value.toString() };
				return values;
			}
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * <pre>
	 * 문자열을 int형으로 변환한 값을 반환 한다.
	 * Examples:
	 * &lt;blockquote&gt;
	 * getInt(&quot;name&quot;);
	 * &lt;/blockquote&gt;
	 * </pre>
	 * 
	 * @param key
	 *            변환 될 문자열
	 * @return 반환되는 int 값
	 * @see com.lamp.common.HttpMap#getInt(String)
	 */
	public int getInt(String key) {
		try {
			return Integer.parseInt(get(key).toString());
		} catch (Exception e) {
			return 0;
		}
	}

	public void setInt(String key, int value) {
		try {
			put(key, new Integer(value));
		} catch (Exception e) {
		}

	}

	/**
	 * @see com.lamp.common.HttpMap#equals(java.lang.String, java.lang.Object)
	 */
	public boolean equals(String key, Object value) {
		try {
			return get(key).equals(value);
		} catch (Exception e) {
			return false;
		}
	}

	public boolean getBoolean(String key) {
		try {
			return ((Boolean) get(key)).booleanValue();
		} catch (Exception e) {
			return false;
		}
	}

	public void setBoolean(String key, boolean value) {
		try {
			put(key, new Boolean(value));
		} catch (Exception e) {
		}

	}

	public void setString(String key, String value) {
		put(key, value);
	}

	public void setStrings(String key, String[] values) {
		put(key, values);
	}

	public boolean isSetKeys(final String keyNames) {
		if (keyNames == null || keyNames.length() < 1) {
			return false;
		}

		String[] keys = StringUtils.split(keyNames, ',');
		Iterator iterator = keySet().iterator();
		int len = keys.length;
		int j = 0;
		while (iterator.hasNext()) {
			Object o = iterator.next();
			for (int i = 0; i < len; i++) {
				if (o.equals(keys[i])) {
					++j;
				}
			}
			if (j == len) {
				return true;
			}
		}
		return false;
	}

	public boolean isSetKeyValues(final String keyNames) {
		if (keyNames == null || keyNames.length() < 1) {
			return false;
		}

		String[] keys = StringUtils.split(keyNames, ',');
		Iterator iterator = entrySet().iterator();
		int len = keys.length;
		int j = 0;
		while (iterator.hasNext()) {
			Map.Entry entry = (Map.Entry) iterator.next();
			for (int i = 0; i < len; i++) {
				if (entry.getKey().equals(keys[i])) {
					if (entry.getValue() != null) {
						++j;
					}
				}
			}
			if (j == len) {
				return true;
			}
		}
		return false;
	}

}
